package com.ruoyi.ai.service;

import com.ruoyi.ai.service.url.DomainService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class IFileService {
    @Autowired
    private DomainService domainService;

    /**
     * 上传文件到本地
     * @param file
     * @return
     */
    public Map<String, String> uploadFileLocal(MultipartFile file){
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = null;
        try {
            fileName = FileUploadUtils.upload(filePath, file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String url = domainService.getUrl() + fileName;
        Map<String, String> data = new HashMap<>();
        data.put("url", url);
        data.put("fileName", fileName);
        data.put("newFileName", FileUtils.getName(fileName));
        data.put("originalFilename", file.getOriginalFilename());
        return data;
    }
}
